//
//  PlayerDetailsViewController.m
//  Ratings
//
//  Created by steven.zhan on 14-9-16.
//  Copyright (c) 2014年 hpxbzz. All rights reserved.
//

#import "PlayerDetailsViewController.h"

@interface PlayerDetailsViewController ()

@end

@implementation PlayerDetailsViewController
{
    NSString *game;
}

@synthesize delegate;
@synthesize playerToEdit;

- (id)initWithCoder:(NSCoder *)aDecoder
{
    if (self=[super initWithCoder:aDecoder]) {
        NSLog(@"init PlayerDetailsViewController");
        game = @"Chess";
    }
    return self;
}

- (void)dealloc
{
    NSLog(@"dealloc PlayerDetailsViewController");
}

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    if (self.playerToEdit != nil) {
        self.title = @"Edit Player";
        self.nameTextField.text = self.playerToEdit.name;
        game = self.playerToEdit.game;
    }
    
    self.detailLabel.text=game;
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.section==0) {
        [self.nameTextField becomeFirstResponder];
    }
}


- (IBAction)cancel:(id)sender
{
    [self.delegate playerDetailsViewControllerDidCancel:self];
}
- (IBAction)done:(id)sender
{
    if (self.playerToEdit != nil)
    {
        self.playerToEdit.name=self.nameTextField.text;
        self.playerToEdit.game = game;
        [self.delegate playerDetailsViewControllerDidEdit:self didEditPlayer:self.playerToEdit];
    }
    else
    {
        Player *player = [[Player alloc] init];
        player.name = self.nameTextField.text;
        player.game = game;
        player.rating = 1;
        [self.delegate playerDetailsViewControllerDidSave:self didAddPlayer:player];
    }
}

- (void)gamePickerViewController:(GamePickerViewController*)controller
                   didSelectGame:(NSString*)theGame
{
    game = theGame;
    self.detailLabel.text=game;
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"PickGame"]) {
        GamePickerViewController *gamePickerViewController = segue.destinationViewController;
        gamePickerViewController.delegate = self;
        gamePickerViewController.game = game;
    }
}

@end
